/*
 * KrakencitoView.java
 */

package krakencito;

import java.awt.Color;
import javax.swing.tree.TreePath;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

import java.io.*;
import java.util.*;
import java.net.*;
import java.util.regex.*;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;


/**
 * The application's main frame.
 */
public class KrakencitoView extends FrameView {

    public KrakencitoView(SingleFrameApplication app) {
        super(app);
       
        initComponents();
       
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = KrakencitoApp.getApplication().getMainFrame();
            aboutBox = new KrakencitoAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        KrakencitoApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        mainPanel = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        AdminPanel = new javax.swing.JPanel();
        adminTxtFld = new javax.swing.JTextField();
        bt_AgregarBloq = new javax.swing.JButton();
        bt_EliminarBloq = new javax.swing.JButton();
        bt_EliminarTodosBloq = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        br_GuardarBloq = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        administrableTree = new javax.swing.JTree();
        EstadPanel = new javax.swing.JPanel();
        topSites = new javax.swing.JRadioButton();
        userSites = new javax.swing.JRadioButton();
        userSitesTxtFld = new javax.swing.JTextField();
        bt_listarEstadisticas = new javax.swing.JButton();
        denied = new javax.swing.JRadioButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaEstadisticas = new javax.swing.JList();
        AboutPanel = new javax.swing.JPanel();
        imageLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        StatusApp = new javax.swing.JLabel();
        grupoBotonesEstadisticas = new javax.swing.ButtonGroup();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(krakencito.KrakencitoApp.class).getContext().getResourceMap(KrakencitoView.class);
        mainPanel.setBackground(resourceMap.getColor("mainPanel.background")); // NOI18N
        mainPanel.setMaximumSize(new java.awt.Dimension(1024, 350));
        mainPanel.setMinimumSize(new java.awt.Dimension(0, 350));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(1024, 350));

        jTabbedPane1.setMinimumSize(new java.awt.Dimension(102, 64));
        jTabbedPane1.setName("jTabbedPane1"); // NOI18N
        jTabbedPane1.setPreferredSize(new java.awt.Dimension(808, 350));
        jTabbedPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTabbedPane1MouseClicked(evt);
            }
        });

        AdminPanel.setName("AdminPanel"); // NOI18N

        adminTxtFld.setText(resourceMap.getString("text")); // NOI18N
        adminTxtFld.setName(""); // NOI18N

        bt_AgregarBloq.setText(resourceMap.getString("bt_AgregarBloq.text")); // NOI18N
        bt_AgregarBloq.setName("bt_AgregarBloq"); // NOI18N
        bt_AgregarBloq.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bt_AgregarBloqMouseClicked(evt);
            }
        });

        bt_EliminarBloq.setText(resourceMap.getString("bt_EliminarBloq.text")); // NOI18N
        bt_EliminarBloq.setName("bt_EliminarBloq"); // NOI18N
        bt_EliminarBloq.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bt_EliminarBloqMouseClicked(evt);
            }
        });

        bt_EliminarTodosBloq.setText(resourceMap.getString("bt_EliminarTodosBloq.text")); // NOI18N
        bt_EliminarTodosBloq.setName("bt_EliminarTodosBloq"); // NOI18N
        bt_EliminarTodosBloq.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bt_EliminarTodosBloqMouseClicked(evt);
            }
        });

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        br_GuardarBloq.setText(resourceMap.getString("br_GuardarBloq.text")); // NOI18N
        br_GuardarBloq.setName("br_GuardarBloq"); // NOI18N
        br_GuardarBloq.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                br_GuardarBloqMouseClicked(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Prohibidas");
        administrableTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        administrableTree.setName("administrableTree"); // NOI18N
        jScrollPane1.setViewportView(administrableTree);

        javax.swing.GroupLayout AdminPanelLayout = new javax.swing.GroupLayout(AdminPanel);
        AdminPanel.setLayout(AdminPanelLayout);
        AdminPanelLayout.setHorizontalGroup(
            AdminPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AdminPanelLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(AdminPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)
                    .addGroup(AdminPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(AdminPanelLayout.createSequentialGroup()
                            .addComponent(adminTxtFld, javax.swing.GroupLayout.PREFERRED_SIZE, 502, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(bt_AgregarBloq, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(AdminPanelLayout.createSequentialGroup()
                            .addComponent(bt_EliminarBloq, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(bt_EliminarTodosBloq, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(br_GuardarBloq, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(16, 16, 16))
        );
        AdminPanelLayout.setVerticalGroup(
            AdminPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AdminPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(AdminPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bt_AgregarBloq, 0, 0, Short.MAX_VALUE)
                    .addComponent(adminTxtFld, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(AdminPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_EliminarBloq, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_EliminarTodosBloq, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(br_GuardarBloq, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jTabbedPane1.addTab(resourceMap.getString("AdminPanel.TabConstraints.tabTitle"), AdminPanel); // NOI18N

        EstadPanel.setName("EstadPanel"); // NOI18N

        grupoBotonesEstadisticas.add(topSites);
        topSites.setText(resourceMap.getString("topSites.text")); // NOI18N
        topSites.setName("topSites"); // NOI18N
        topSites.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                topSitesMouseClicked(evt);
            }
        });

        grupoBotonesEstadisticas.add(userSites);
        userSites.setText(resourceMap.getString("userSites.text")); // NOI18N
        userSites.setName("userSites"); // NOI18N

        userSitesTxtFld.setText(resourceMap.getString("userSitesTxtFld.text")); // NOI18N
        userSitesTxtFld.setToolTipText(resourceMap.getString("userSitesTxtFld.toolTipText")); // NOI18N
        userSitesTxtFld.setName("userSitesTxtFld"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, userSites, org.jdesktop.beansbinding.ELProperty.create("${selected}"), userSitesTxtFld, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        bt_listarEstadisticas.setText(resourceMap.getString("bt_listarEstadisticas.text")); // NOI18N
        bt_listarEstadisticas.setName("bt_listarEstadisticas"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, userSites, org.jdesktop.beansbinding.ELProperty.create("${selected}"), bt_listarEstadisticas, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        bt_listarEstadisticas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bt_listarEstadisticasMouseClicked(evt);
            }
        });

        grupoBotonesEstadisticas.add(denied);
        denied.setText(resourceMap.getString("denied.text")); // NOI18N
        denied.setName("denied"); // NOI18N
        denied.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                deniedMouseClicked(evt);
            }
        });

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        listaEstadisticas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listaEstadisticas.setName("listaEstadisticas"); // NOI18N
        jScrollPane2.setViewportView(listaEstadisticas);

        javax.swing.GroupLayout EstadPanelLayout = new javax.swing.GroupLayout(EstadPanel);
        EstadPanel.setLayout(EstadPanelLayout);
        EstadPanelLayout.setHorizontalGroup(
            EstadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(EstadPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(EstadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 692, Short.MAX_VALUE)
                    .addGroup(EstadPanelLayout.createSequentialGroup()
                        .addComponent(denied)
                        .addGap(18, 18, 18)
                        .addComponent(topSites)
                        .addGap(18, 18, 18)
                        .addComponent(userSites)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(userSitesTxtFld, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_listarEstadisticas, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        EstadPanelLayout.setVerticalGroup(
            EstadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(EstadPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(EstadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(denied)
                    .addComponent(topSites)
                    .addComponent(userSites)
                    .addComponent(userSitesTxtFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_listarEstadisticas, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab(resourceMap.getString("EstadPanel.TabConstraints.tabTitle"), EstadPanel); // NOI18N

        AboutPanel.setBackground(resourceMap.getColor("AboutPanel.background")); // NOI18N
        AboutPanel.setName("AboutPanel"); // NOI18N

        imageLabel.setIcon(resourceMap.getIcon("imageLabel.icon")); // NOI18N
        imageLabel.setName("imageLabel"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        javax.swing.GroupLayout AboutPanelLayout = new javax.swing.GroupLayout(AboutPanel);
        AboutPanel.setLayout(AboutPanelLayout);
        AboutPanelLayout.setHorizontalGroup(
            AboutPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AboutPanelLayout.createSequentialGroup()
                .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 397, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(50, 50, 50)
                .addGroup(AboutPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(AboutPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AboutPanelLayout.createSequentialGroup()
                            .addGroup(AboutPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                            .addGap(34, 34, 34)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AboutPanelLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(72, 72, 72)))
                .addGap(59, 59, 59))
        );
        AboutPanelLayout.setVerticalGroup(
            AboutPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AboutPanelLayout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(53, 53, 53)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(61, Short.MAX_VALUE))
            .addGroup(AboutPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 351, Short.MAX_VALUE)
                .addGap(50, 50, 50))
        );

        jTabbedPane1.addTab(resourceMap.getString("AboutPanel.TabConstraints.tabTitle"), AboutPanel); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 724, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName(resourceMap.getString("jTabbedPane1.AccessibleContext.accessibleName")); // NOI18N

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        StatusApp.setText(resourceMap.getString("StatusApp.text")); // NOI18N
        StatusApp.setName("StatusApp"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(statusMessageLabel))
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(StatusApp)))
                .addGap(13, 13, 13)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 665, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                        .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(statusAnimationLabel)
                        .addContainerGap())))
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .addComponent(StatusApp)
                .addGap(12, 12, 12))
        );

        setComponent(mainPanel);
        setStatusBar(statusPanel);

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void jTabbedPane1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTabbedPane1MouseClicked
        // TODO add your handling code here:
        
        StatusApp.setText("Standby");
        StatusApp.setForeground(Color.BLACK);
        
        construirArbolProhibidas();
        //.add(new JScrollPane(tree));
    }//GEN-LAST:event_jTabbedPane1MouseClicked

    private void deniedMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deniedMouseClicked
        // TODO add your handling code here:
        
        listaEstadisticas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = (String[]) ImprimirDenied(Estadistica.denied()).toArray(new String[0]);
            
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
            
            public List<String> ImprimirDenied(List<Estadistica> denied){
                List<String> s = new LinkedList();
                System.out.println("Intentos de conexion denegados:");
                InetAddress addr;
                String hostname;
                String canhost;
                System.out.println("Pos | Nombre del Sitio | Intentos de conexion");
                for (int i = 0; i < denied.size(); i++) {
                    //Obtiene el nombre del sitio
                    try {
                        addr = InetAddress.getByName(denied.get(i).getHost());
                        hostname = addr.getHostName();
                        canhost = addr.getCanonicalHostName();
                    } catch (UnknownHostException e) {
                        hostname = denied.get(i).getHost();
                        canhost = hostname;
                    }
                    //System.out.println((i + 1) + ".- | " + canhost + " | " + denied.get(i).getNumVisitas());
                    s.add(canhost + " | " + denied.get(i).getNumVisitas());
                }
                return s;
            }
        });
    }//GEN-LAST:event_deniedMouseClicked

    private void bt_listarEstadisticasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bt_listarEstadisticasMouseClicked
        // TODO add your handling code here:
        
        if (userSitesTxtFld.getText().equals("")) {
            StatusApp.setForeground(Color.RED);
            StatusApp.setText("Error: Hay campos vacios");
            return;
        }
        
        listaEstadisticas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = (String[]) ImprimirUserSites(Estadistica.UserSites(userSitesTxtFld.getText()), userSitesTxtFld.getText()).toArray(new String[0]);
            
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
            
            public List<String> ImprimirUserSites(List<Estadistica> usersites, String usuario){
                //StatusApp.setForeground(Color.GREEN);
                //StatusApp.setText("Calculando estadisticas");
                List<String> s = new LinkedList();
                System.out.println("Sitios visitados por el usuario " + usuario + ":");
                InetAddress addr;
                String hostname;
                System.out.println("Pos | Nombre del Sitio | Numero de Visitas");
                for (int i = 0; i < usersites.size(); i++) {
                    //Obtiene el nombre del sitio
                    try {
                        addr = InetAddress.getByName(usersites.get(i).getHost());
                        hostname = addr.getHostName();
                    } catch (UnknownHostException e) {
                        hostname = usersites.get(i).getHost();
                    }
                    //System.out.println((i + 1) + ".- | " + hostname + " | " + usersites.get(i).getNumVisitas());
                    s.add(hostname + " | " + usersites.get(i).getNumVisitas());
                }
                return s;
            }
        });
    }//GEN-LAST:event_bt_listarEstadisticasMouseClicked

    private void topSitesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_topSitesMouseClicked
        // TODO add your handling code here:
        
        listaEstadisticas.setModel(new javax.swing.AbstractListModel() {
            String[] strings =  (String[]) (ImprimirTopSites(Estadistica.TopSites()).toArray(new String[0])); //{ "Item 100", "Item 200", "Item 300", "Item 400", "Item 500" };
            
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
            
            public List<String> ImprimirTopSites(List<Estadistica> topSites) {
                List<String> s = new LinkedList();
                System.out.println("Sitios mas visitados:");
                InetAddress addr;
                String hostname;
                System.out.println("Pos | Nombre del Sitio | Numero de Visitas");
                for (int i = 0; i < topSites.size(); i++) {
                    //Obtiene el nombre del sitio
                    try {
                        addr = InetAddress.getByName(topSites.get(i).getHost());
                        hostname = addr.getHostName();
                    } catch (UnknownHostException e) {
                        hostname = topSites.get(i).getHost();
                    }
                    //System.out.println((i + 1) + ".- | " + hostname + " | " + topSites.get(i).getNumVisitas());
                    s.add(hostname + " | " + topSites.get(i).getNumVisitas());
                }
                System.out.println("Listo el pavo");
                return s;
            }
        });
}//GEN-LAST:event_topSitesMouseClicked

    private void br_GuardarBloqMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_br_GuardarBloqMouseClicked
        // TODO add your handling code here:
        
        if (JOptionPane.showConfirmDialog(null, "Realmente desea guardar los cambios en disco?\nNo se puede deshacer esta decision", "Save the Kraken!!!", JOptionPane.OK_CANCEL_OPTION) == 0) {
            //KrakencitoApp.prohibidas = new Hashtable<String, List<String>>();
            KrakencitoApp.ArmarXML(KrakencitoApp.prohibidas);
            JOptionPane.showMessageDialog(null, "La lista de direcciones prohibidas en disco ha sido actualizada","Save the Kraken!!!", 1);
        }
        
        //
}//GEN-LAST:event_br_GuardarBloqMouseClicked

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(null, "Realmente desea cargar la lista de bloqueados en disco?\nPerdera cualquier cambio sin guardar que haya hecho", "Load the Kraken!!!", JOptionPane.OK_CANCEL_OPTION) == 0) {
            //KrakencitoApp.prohibidas = new Hashtable<String, List<String>>();
            KrakencitoApp.prohibidas = KrakencitoApp.LeerProhibidas();
            JOptionPane.showMessageDialog(null, "La lista de direcciones prohibidas en memoria ha sido actualizada","Load the Kraken!!!", 1);
            construirArbolProhibidas();
        }
}//GEN-LAST:event_jButton4MouseClicked

    private void bt_EliminarTodosBloqMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bt_EliminarTodosBloqMouseClicked
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(null, "Realmente desea eliminar todos los sitios prohibidos?\nNo se puede deshacer esta decision", "Release the Kraken!!!", JOptionPane.OK_CANCEL_OPTION) == 0) {
            JOptionPane.showMessageDialog(null, "La lista de direcciones prohibidas ha sido limpiada","Release the Kraken!!!", 1);
            KrakencitoApp.prohibidas = new Hashtable<String, List<String>>();
            construirArbolProhibidas();
        }
        
        //showMessageDialog(null, "I am happy.");
}//GEN-LAST:event_bt_EliminarTodosBloqMouseClicked

    private void bt_EliminarBloqMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bt_EliminarBloqMouseClicked
        // TODO add your handling code here:
        
        TreePath x = administrableTree.getSelectionPath();
        if (x==null)
            return;
        //administrableTree.removeSelectionPath(x);
        String s = x.toString();
        System.out.println(s);
        
        int a = s.indexOf(',');
        int b = s.lastIndexOf(',');
        
        String subS = "";
        if (a==b) //Se esta eliminando una direccion y no un subdirectorio
        {
            subS = s.substring(a+2, s.length()-1);
            KrakencitoApp.prohibidas.remove(subS);
        } else {
            //subS = s.substring(a+2,b);
            String subS2 = s.substring(a+2,b);
            subS = s.substring(b+2, s.length()-1);
            
            List<String> pb = KrakencitoApp.prohibidas.get(subS2);
            
            pb.remove(subS);
        }
        
        System.out.println(subS);
        
        construirArbolProhibidas();
}//GEN-LAST:event_bt_EliminarBloqMouseClicked

    private void bt_AgregarBloqMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bt_AgregarBloqMouseClicked
        // TODO add your handling code here:
        TreePath x = administrableTree.getSelectionPath();
        if (x==null)
            return;
        String s = x.toString();
        System.out.println(s);
        
        String input = adminTxtFld.getText();
               
        if (input.equals("")) {
            StatusApp.setForeground(Color.RED);
            StatusApp.setText("Error: Hay campos vacios");
            return;
        }
        
        int a = s.indexOf(',');
        int b = s.lastIndexOf(',');
        
        String ffuuaa = input;
        String subS = "";
        if (a==b) //Se esta agregando una direccion y no un subdirectorio
        {
            subS = s.substring(a+2, s.length()-1);
            
            if (!isIPAddress(input)) {
                //System.out.println("El IP que se desea agregar no es valido");
               
                try {
                    String serv = InetAddress.getByName(input).toString();
                    ffuuaa = serv.substring(serv.indexOf("/") + 1);
                    
                    adminTxtFld.setText("");
                    //return;
                } catch (Exception e) {
                    
                }
            }
            
            
            if (KrakencitoApp.prohibidas.get(ffuuaa)==null) //No existe ese elemento
            {
                List<String> ll = new LinkedList<String>();
                ll.add("/");
                KrakencitoApp.prohibidas.put(ffuuaa, ll);
            } else {
                //System.out.println("El elemento ya existe");
                StatusApp.setText("Ya existe ese elemento");
                StatusApp.setForeground(Color.ORANGE);
                adminTxtFld.setText("");
                return;
            }
            
        } else {
            //subS = s.substring(a+2,b);
            
            if (isIPAddress(input)) {
                StatusApp.setText("Un subdirectorio no es un IP");
                StatusApp.setForeground(Color.RED);
                adminTxtFld.setText("");
                return;
            }
            
            if (input.indexOf(' ')!=-1) {
                //System.out.println("Hey, hey, sin espacios!!");
                StatusApp.setText("Un subdirectorio no puede tener espacios");
                StatusApp.setForeground(Color.RED);
                adminTxtFld.setText("");
                return;
            }
            
            
            String subS2 = s.substring(a+2,b);
            subS = s.substring(b+2, s.length()-1);
            
            List<String> pb = KrakencitoApp.prohibidas.get(subS2);
            
            pb.add("/"+input+"/");
            //adminTxtFld.setText("");
        }
        
        System.out.println(subS);
        
        adminTxtFld.setText("");
        construirArbolProhibidas();
        StatusApp.setForeground(Color.BLACK);
    }//GEN-LAST:event_bt_AgregarBloqMouseClicked

    
    
    

    
    
    
    
    private void construirArbolProhibidas()
    {
        //KrakencitoApp.prohibidas;
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Prohibidas");
        
         for (Enumeration e = KrakencitoApp.prohibidas.keys(); e.hasMoreElements() ;)
         {
             String s = (String) e.nextElement();
             DefaultMutableTreeNode child = new DefaultMutableTreeNode(s);
             
             String[] algo = KrakencitoApp.prohibidas.get(s).toArray(new String[0]);
             
             for (int i = 0; i<algo.length ;i++)
             {
                 child.add(new DefaultMutableTreeNode(algo[i]));
             }
                          
             //child.add(new DefaultMutableTreeNode(KrakencitoApp.prohibidas.get(s)));
             root.add(child);
             
            //root.add(new DefaultMutableTreeNode(KrakencitoApp.prohibidas.get(e.nextElement())));
            //sDummy = (folders.get(e.nextElement())).getFPath(fHash);
            //if (!sDummy.equals(""))
            //   return (this.folderName + this.separator + sDummy);
         }
               
        /*
         DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
        root.add(child1);
        
        DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
        root.add(child2);
        */
        
        administrableTree = new JTree(root);
        jScrollPane1.setViewportView(administrableTree);    
    }
    
    private boolean isIPAddress(String str) {  
      Pattern ipPattern = Pattern.compile("^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$");
        //"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");  
      return ipPattern.matcher(str).matches();  
    }  
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel AboutPanel;
    private javax.swing.JPanel AdminPanel;
    private javax.swing.JPanel EstadPanel;
    private javax.swing.JLabel StatusApp;
    private javax.swing.JTextField adminTxtFld;
    private javax.swing.JTree administrableTree;
    private javax.swing.JButton br_GuardarBloq;
    private javax.swing.JButton bt_AgregarBloq;
    private javax.swing.JButton bt_EliminarBloq;
    private javax.swing.JButton bt_EliminarTodosBloq;
    private javax.swing.JButton bt_listarEstadisticas;
    private javax.swing.JRadioButton denied;
    private javax.swing.ButtonGroup grupoBotonesEstadisticas;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JList listaEstadisticas;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JRadioButton topSites;
    private javax.swing.JRadioButton userSites;
    private javax.swing.JTextField userSitesTxtFld;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
    
}
